<template>
  <div>
<el-breadcrumb separator-class="el-icon-arrow-right">
  <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
  <el-breadcrumb-item>用户管理</el-breadcrumb-item>
  <el-breadcrumb-item>用户列表</el-breadcrumb-item>
</el-breadcrumb>
<!--  -->

<el-card>
  
<!-- 搜索 -->


<el-row :gutter="20">
  <el-col :span="8">
    <div class="grid-content bg-purple">
      <el-input placeholder="请输入内容" v-model="keyword" class="input-with-select">
        <el-button slot="append" icon="el-icon-search" @click="getdata"></el-button>
      </el-input>
    </div>
  </el-col>
  <el-col :span="16">
    <div class="grid-content bg-purple">
      <el-button type="primary" @click="add">添加用户</el-button>
    </div>
  </el-col>
</el-row>
<!-- 表格列表 -->
 <el-table
    :data="users"
    border
    style="width: 100%">
    <el-table-column
      type="index"
      label="#">
    </el-table-column>
    <el-table-column
      prop="username"
      label="姓名"
      width="180">
    </el-table-column>
    <el-table-column
      prop="email"
      label="邮箱"
      width="180">
    </el-table-column>
    <el-table-column
      prop="mobile"
      label="电话"
      width="180">
    </el-table-column>
    <el-table-column
      prop="role_name"
      label="角色"
      width="180">
    </el-table-column>
    <el-table-column
      prop="mg_state"
      label="状态"
      width="180">

<template slot-scope="scope">
  <el-switch
  v-model="scope.row.mg_state"
  active-color="#13ce66"
  @change="updatestate(scope.row)"
  inactive-color="#ff4949">
</el-switch>
</template>


    </el-table-column>
    <el-table-column label="操作">
<template slot-scope="scope">
        <el-button
          size="mini"
          @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
        <el-button
          size="mini"
          type="danger"
          @click="handleDelete(scope.$index, scope.row)">删除</el-button>
      </template>

    </el-table-column>
  </el-table>

<!-- 分页 -->

 <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagenum"
      :page-sizes="[1, 2, 3, 4]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

</el-card>

<!-- 添加弹框 -->

<el-dialog title="添加用户" :visible.sync="dialogFormVisible">
   <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
  <el-form-item label="用户名" prop="username">
    <el-input v-model="ruleForm.username"></el-input>
  </el-form-item>
  <el-form-item label="密码" prop="password">
    <el-input v-model="ruleForm.password"></el-input>
  </el-form-item>
  <el-form-item label="邮箱" prop="email">
    <el-input v-model="ruleForm.email"></el-input>
  </el-form-item>
  <el-form-item label="手机" prop="mobile">
    <el-input v-model="ruleForm.mobile"></el-input>
  </el-form-item>


</el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible = false">取 消</el-button>
    <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
  </div>
</el-dialog>



<!-- 编辑 -->

<el-dialog title="修改用户" :visible.sync="dialogFormVisible1">
   <el-form :model="ruleForm1" :rules="rules1" ref="ruleForm1" label-width="100px" class="demo-ruleForm">
  <el-form-item label="用户名" prop="username">
    <el-input v-model="ruleForm1.username" disabled></el-input>
  </el-form-item>
  <el-form-item label="密码" prop="password">
    <el-input v-model="ruleForm1.password"></el-input>
  </el-form-item>
  <el-form-item label="邮箱" prop="email">
    <el-input v-model="ruleForm1.email"></el-input>
  </el-form-item>
  <el-form-item label="手机" prop="mobile">
    <el-input v-model="ruleForm1.mobile"></el-input>
  </el-form-item>


</el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible1 = false">取 消</el-button>
    <el-button type="primary" @click="submitForm1('ruleForm1')">确 定</el-button>
  </div>
</el-dialog>


  </div>
</template>

<script>
import checkmobile from '../../assets/api'
  export default {
    data() {
     
      return {
        dialogFormVisible:false,
        ruleForm: {
          username: '',
          password:"",
          email:"",
          mobile:""
        
        },
        rules: {
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '请输入活动名称', trigger: 'blur' },
            { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
          ],
          mobile: [
            { required: true, message: '请输入电话', trigger: 'blur' },
            {validator:checkmobile,trigger:"blur"}
            // { min: 11, max: 11, message: '电话格式不正确', trigger: 'blur' }
          ],
        },
        // 修改
        dialogFormVisible1:false,
        ruleForm1: {
          username: '',
          password:"",
          email:"",
          mobile:""
        
        },
        rules1: {
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '请输入活动名称', trigger: 'blur' },
            { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
          ],
          mobile: [
            { required: true, message: '请输入电话', trigger: 'blur' },
            {validator:checkmobile,trigger:"blur"}
            // { min: 11, max: 11, message: '电话格式不正确', trigger: 'blur' }
          ],
        },
        keyword: "",
        users:[],
        pagenum:1,   //当前页
        pagesize:3,   //每页条数
        total:0    //总页码
      }
    },
    created(){
      this.getdata()
    },
    methods: {
      updatestate(row){
          console.log(row)
          // 执行修改的请求
          this.$axios.put(`users/${row.id}/state/${row.mg_state}`)
          .then(res=>{
            console.log(res,"zt")
            if(res.meta.status==200){
              this.$message(res.meta.msg)
            }else{
              // 设置状态伪原来的状态
              row.mg_state=!row.mg_state
            }
          })
      },
      submitForm1(formName) {   //点击弹框的确定按钮  修改
        this.$refs[formName].validate((valid) => {
          if (valid) {
            console.log(this.ruleForm1)
            // 
            this.$axios.put(`users/${this.ruleForm1.id}`,this.ruleForm1)
            .then(res=>{
              console.log(res,"xg")
              if(res.meta.status==200){
                // 提示更新成功
                this.$message(res.meta.msg)
                // 重新渲染
                this.getdata()
                // 关闭模态框
                this.dialogFormVisible1=false
              }
            })

          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      submitForm(formName) {   //点击弹框的确定按钮
        this.$refs[formName].validate((valid) => {
          if (valid) {
            // alert('submit!');
            this.$axios.post('users',this.ruleForm)
            .then(res=>{
              console.log(res,"tj")
              if(res.meta.status==201){
                this.$message(res.meta.msg)
                this.dialogFormVisible=false
                this.getdata()
              }else{
                this.$message(res.meta.msg)
              }
            })



          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      add(){
        // 点击添加按钮
        this.dialogFormVisible=true

      },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
        this.pagesize=val
        this.getdata()
      },
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
        this.pagenum=val
        this.getdata()
      },
      getdata(){
         this.$axios.get('users',{
           params:{
             query:this.keyword,
             pagenum:this.pagenum,
             pagesize:this.pagesize
           }
         })
         .then(res=>{
           console.log(res,"yoh")
           this.users=res.data.users
          //  总数量
          this.total=res.data.total
         })
      },
      handleEdit(index, row) {
        // index 下标   row 当前行数据
        console.log(index, row);
        // 赋值
        this.ruleForm1=row
        // 弹框显示
        this.dialogFormVisible1=true
      },
      handleDelete(index, row) {   //删除
        console.log(index, row);
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
         
         this.$axios.delete('users/'+row.id)
         .then(res=>{
           console.log(res)
           if(res.meta.status==200){
             this.getdata()
             this.$message("删除成功")
           }
           
         })



        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
      }
    },
  }
</script>

<style  scoped>
.el-card{
  margin-top: 15px;
}


</style>